!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine az_gen (az, bx, by, x, y, nx, ny, az_info)
!==================================================================================================================================|

   implicit none

   integer(4), intent(in) :: nx, ny

   real(8), dimension(2) :: az_info
   real(8), dimension(nx), intent(in) :: x
   real(8), dimension(ny), intent(in) :: y
   real(8), dimension(nx, ny), intent(in) :: bx, by
   real(8), dimension(nx, ny), intent(inout) :: az

   integer(4) :: i, j, ip1, jp1, im1, jm1

   real(8), dimension(nx, ny - 1) :: bxh
   real(8), dimension(nx - 1, ny) :: byh

!----------------------------------------------------------------------------------------------------------------------------------|
   do j = 1, ny - 1
   do i = 1, nx
      bxh(i, j) = (bx(i, j) + bx(i, j + 1)) / 2.0d0
   enddo
   enddo

   do j = 1, ny
   do i = 1, nx - 1
      byh(i, j) = (by(i, j) + by(i + 1, j)) / 2.0d0
   enddo
   enddo

   if (nint (az_info(2)) .eq. -1) then
      az(1, 1) = az_info(1)
      do i = 1, nx - 1
         ip1 = i + 1
         az(ip1, 1) = az(i, 1) - (x(ip1) - x(i)) * byh(i, 1)
      enddo
      do i = 1, nx
         do j = 1, ny - 1
            jp1 = j + 1
            az(i, jp1) = az(i, j) + (y(jp1) - y(j)) * bxh(i, j)
         enddo
      enddo
   endif

   if (nint (az_info(2)) .eq. 1) then
      az(nx, ny) = az_info(1)
      do i = nx, 2, -1
         im1 = i - 1
         az(im1, ny) = az(i, ny) - (x(im1) - x(i)) * byh(im1, ny)
      enddo
      do i = nx, 1, -1
         do j = ny, 2, -1
            jm1 = j - 1
            az(i, jm1) = az(i, j) + (y(jm1) - y(j)) * bxh(i, jm1)
         enddo
      enddo
   endif

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine az_gen
